#include "stdio.h"
#include <stdlib.h>
#include <string.h>
#include "lv_compment.h"
#include "esp_log.h"
#include "lv_port_indev.h"
#include "esp_err.h"
#include "gui_time.h"
#include "http.h"
#include "page.h"
#include "myFont.h"
#include "number_temp.h"
#include "number.h"
#include "weather.h"
#include "dht11.h"
INROOM wp;
extern struct DATA_WEATHER weather_data[3];
extern const struct LV_WEATHER lv_weather[40];
extern struct _blbl_sys_t blbl_sys_t;
#define ANIEND  while(lv_anim_count_running())lv_task_handler();
LV_IMG_DECLARE(blbl22);
LV_IMG_DECLARE(blbl33);
LV_FONT_DECLARE(myFont);
LV_FONT_DECLARE(number);
LV_FONT_DECLARE(number_temp);
extern lv_obj_t* scr;
extern lv_obj_t* scr_body;
static lv_style_t style1;
/*****主体****/
lv_obj_t* cont_time,*cont_temp,*cont_fensi;
/*****时间****/
lv_obj_t* label_time1,*label_time1_shi,*label_time1_fen;
lv_obj_t* label_time2;
lv_task_t * task_time;
extern compment_t c22,c33;
/*****粉丝****/
lv_obj_t* label_fensi;
lv_obj_t* label_guanzhu;
/*****温湿度****/
lv_obj_t* cont_temp_number;
lv_obj_t* label_temp_number;
compment_t pcf,pct,pcth;//动画结构体

static void update_weather(void);
void page_task_time(lv_task_t * task)
{
    static uint8_t i=0,h=0;
    if(i%2==0)
        lv_obj_set_hidden(label_time1,1);
    else
        lv_obj_set_hidden(label_time1,0);

    if(i%20==0)//10s
    {
     update_temp(); //更新温湿度         
     update_time();  //更新时间
    }

    if(i%120==0)//一分钟
    {
        i=0;
        h++;
        if(h%3==0)//3分钟，更新粉丝数
        update_fensi();
        if(h==h*60*8)//8小时
        {
            update_weather();//更新天气
            h=0;
        }
    }
    i++;

}
void update_time()
{
    char now_time[40];
    sprintf((char *)now_time,"#FFA500 %02d#",blbl_sys_t.timeinfo.tm_hour);
    lv_label_set_text(label_time1_shi,(const char *)now_time );
    sprintf((char *)now_time,"#FFA500 %02d#",blbl_sys_t.timeinfo.tm_min);
    lv_label_set_text(label_time1_fen, (const char *)now_time);
        //星期与日历
    switch (blbl_sys_t.timeinfo.tm_wday)
	{
	case 0:
	    sprintf((char *)now_time,"#FFC0CB 星期日%02d/%02d#",blbl_sys_t.timeinfo.tm_mon+1,blbl_sys_t.timeinfo.tm_mday);
		break;
	case 1:
		sprintf((char *)now_time,"#FFC0CB 星期一%02d/%02d#",blbl_sys_t.timeinfo.tm_mon+1,blbl_sys_t.timeinfo.tm_mday);
		break;
	case 2:
		sprintf((char *)now_time,"#FFC0CB 星期二%02d/%02d#",blbl_sys_t.timeinfo.tm_mon+1,blbl_sys_t.timeinfo.tm_mday);
		break;
	case 3:
		sprintf((char *)now_time,"#FFC0CB 星期三%02d/%02d#",blbl_sys_t.timeinfo.tm_mon+1,blbl_sys_t.timeinfo.tm_mday);
		break;
	case 4:
		sprintf((char *)now_time,"#FFC0CB 星期四%02d/%02d#",blbl_sys_t.timeinfo.tm_mon+1,blbl_sys_t.timeinfo.tm_mday);
		break;
	case 5:
		sprintf((char *)now_time,"#FFC0CB 星期五%02d/%02d#",blbl_sys_t.timeinfo.tm_mon+1,blbl_sys_t.timeinfo.tm_mday);
		break;
	case 6:
		sprintf((char *)now_time,"#FFC0CB 星期六%02d/%02d#",blbl_sys_t.timeinfo.tm_mon+1,blbl_sys_t.timeinfo.tm_mday);
		break;

	default:
		sprintf((char *)now_time,"星期日%02d/%02d",blbl_sys_t.timeinfo.tm_mon+1,blbl_sys_t.timeinfo.tm_mday);
		break;
		break;
	}
    lv_label_set_text(label_time2, (const char *)now_time);
    
}
void page1_time(compment_t *comp)
{
    cont_time = lv_cont_create(scr_body, NULL);
    lv_cont_set_fit2(cont_time,LV_FIT_NONE,LV_FIT_NONE);
    lv_obj_set_size(cont_time,120,50);
    lv_obj_set_pos(cont_time,0,10);
    lv_obj_add_style(cont_time, LV_BTN_PART_MAIN, &style1);      /*Default button style*/
    //lv_cont_set_layout(cont_time, LV_LAYOUT_CENTER);
    //小时
    label_time1_shi=lv_label_create(cont_time,NULL);
    lv_label_set_recolor(label_time1_shi, true);
    lv_obj_set_pos(label_time1_shi,20,5);
    lv_label_set_align(label_time1_shi, LV_LABEL_ALIGN_CENTER);       /*Center aligned lines*/
    lv_obj_set_style_local_text_font(label_time1_shi,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&number);

    //点
    label_time1=lv_label_create(cont_time,NULL);
    lv_label_set_recolor(label_time1, true);
    lv_obj_set_pos(label_time1,50,2);
    lv_label_set_align(label_time1, LV_LABEL_ALIGN_CENTER);       /*Center aligned lines*/
    lv_obj_set_style_local_text_font(label_time1,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&number);
    lv_label_set_text(label_time1, "#FFA500 :#");
    //分钟
    label_time1_fen=lv_label_create(cont_time,NULL);
    lv_label_set_recolor(label_time1_fen, true);
    lv_obj_set_pos(label_time1_fen,60,5);
    lv_label_set_align(label_time1_fen, LV_LABEL_ALIGN_CENTER);       /*Center aligned lines*/
    lv_obj_set_style_local_text_font(label_time1_fen,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&number);


    //星期及日期
    label_time2=lv_label_create(cont_time,NULL);
    lv_label_set_recolor(label_time2, true);
    lv_obj_set_pos(label_time2,10,30);
    lv_label_set_align(label_time2, LV_LABEL_ALIGN_CENTER);       /*Center aligned lines*/
    lv_obj_set_style_local_text_font(label_time2,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&myFont);

    update_time();//更新数据
    //动画
    comp->start=-120;
    comp->end=0;
    comp->obj=cont_time;
    comp->start_t=600;
    comp->end_t=600;
    comp->cb=lv_anim_path_ease_out;
    comp->funcb=(void *)lv_obj_set_x;
    comp->next=NULL;
}

void update_fensi()
{
    char now_time[40];
    sprintf((char *)now_time,"粉丝:%d\n关注:%d",blbl_sys_t.wp.follow,blbl_sys_t.wp.following);
    lv_label_set_text(label_fensi, (const char *)now_time);
}
void page1_fensi(compment_t *comp)
{

    cont_fensi = lv_cont_create(scr_body, NULL);
    lv_cont_set_fit2(cont_fensi,LV_FIT_NONE,LV_FIT_NONE);
    lv_obj_set_size(cont_fensi,80,48);
    lv_obj_set_pos(cont_fensi,80,135-40-16);
    lv_obj_add_style(cont_fensi, LV_BTN_PART_MAIN, &style1);      /*Default button style*/
    lv_cont_set_layout(cont_fensi, LV_LAYOUT_CENTER);
    //主题
    static lv_style_t label_fensi_style;
    lv_style_init(&label_fensi_style);
    lv_style_set_text_opa(&label_fensi_style, LV_STATE_DEFAULT, LV_OPA_50);
    lv_style_set_text_color(&label_fensi_style, LV_STATE_DEFAULT, LV_COLOR_YELLOW);
    //粉丝
    label_fensi=lv_label_create(cont_fensi,NULL);
    lv_label_set_recolor(label_fensi, true);
    lv_label_set_align(label_fensi, LV_LABEL_ALIGN_CENTER);       /*Center aligned lines*/
    lv_obj_set_style_local_text_font(label_fensi,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&myFont);
    lv_obj_add_style(label_fensi, LV_LABEL_PART_MAIN, &label_fensi_style);
    update_fensi();

    //动画
    comp->start=135-16;
    comp->end=135-16-48;
    comp->obj=cont_fensi;
    comp->start_t=600;
    comp->end_t=600;
    comp->cb=lv_anim_path_ease_out;
    comp->funcb=(void *)lv_obj_set_y;
    comp->next=NULL;
}
void update_temp()
{
    //更新温湿度
    char now_time[40];
    sprintf(now_time,"%d ℃\n%d %%",blbl_sys_t.dht.temperature,blbl_sys_t.dht.humidity);
    lv_label_set_text(label_temp_number,(const char *) now_time);
}
static void update_weather()
{
     //天气图片
   // char file[30];
    uint8_t i=weather_data[0].code_day; 
    //ESP_LOGI("WEATHER","更新天气");

    lv_img_dsc_t *p=(lv_img_dsc_t *)lv_weather[i].img_dsc;
    static lv_obj_t *imgtq;
    imgtq = lv_img_create(cont_temp,NULL);
    lv_img_set_src(imgtq,p);

}
void page1_temp(compment_t *comp)
{

    //主cont体
    cont_temp = lv_cont_create(scr_body, NULL);
    lv_cont_set_fit2(cont_temp,LV_FIT_NONE,LV_FIT_NONE);
    lv_obj_set_size(cont_temp,120,50);
    lv_obj_set_pos(cont_temp,140,10);
    lv_obj_add_style(cont_temp, LV_BTN_PART_MAIN, &style1);      /*Default button style*/

    //温湿度cont
    cont_temp_number = lv_cont_create(cont_temp, NULL);
    lv_cont_set_fit2(cont_temp_number,LV_FIT_NONE,LV_FIT_NONE);
    lv_obj_set_size(cont_temp_number,72,50);
    lv_obj_set_pos(cont_temp_number,48,0);
    lv_obj_add_style(cont_temp_number, LV_BTN_PART_MAIN, &style1);      /*Default button style*/
    lv_cont_set_layout(cont_temp_number, LV_LAYOUT_CENTER);
    //风格
    static lv_style_t label_temp_style;
    lv_style_init(&label_temp_style);
    lv_style_set_text_opa(&label_temp_style, LV_STATE_DEFAULT, LV_OPA_50);
    lv_style_set_text_color(&label_temp_style, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    //百分比
    label_temp_number=lv_label_create(cont_temp_number,NULL);
    lv_obj_set_pos(label_temp_number,0,5);
    lv_label_set_align(label_temp_number, LV_LABEL_ALIGN_CENTER);       /*Center aligned lines*/
    lv_obj_set_style_local_text_font(label_temp_number,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&number_temp);
    lv_obj_add_style(label_temp_number, LV_LABEL_PART_MAIN, &label_temp_style);

    update_temp();
    update_weather();
    //加载动画
    comp->start=240;
    comp->end=120;
    comp->obj=cont_temp;
    comp->start_t=600;
    comp->end_t=600;
    comp->cb=lv_anim_path_ease_out;
    comp->funcb=(void *)lv_obj_set_x;
    comp->next=NULL;
}
void page1_blbl()
{

    static lv_obj_t * img22;
    img22 = lv_img_create(scr_body,NULL);
    lv_img_set_src(img22,&blbl22);
    lv_obj_set_pos(img22,-120,135-16-48);

    static lv_obj_t * img33;
    img33= lv_img_create(scr_body,NULL);
    lv_img_set_src(img33,&blbl33);
    lv_obj_set_pos(img33,240,135-16-48);

    //动画

    c22.start=-120;
    c22.end=0;
    c22.obj=img22;
    c22.start_t=1000;
    c22.end_t=600;
    c22.cb=lv_anim_path_ease_out;
    c22.funcb=(void *)lv_obj_set_x;
    c22.next=NULL;

    c33.start=240;
    c33.end=160;
    c33.obj=img33;
    c33.start_t=1000;
    c33.end_t=600;
    c33.cb=lv_anim_path_ease_out;
    c33.funcb=(void *)lv_obj_set_x;
    c33.next=NULL;

}

void page1_load()
{
    lv_style_set_bg_color(&style1, LV_STATE_DEFAULT, LV_COLOR_BLACK);//璁剧疆灞忓箷鑳屾櫙
    lv_style_set_border_color(&style1, LV_STATE_DEFAULT, LV_COLOR_BLACK);


    page1_blbl();
    page1_temp(&pcth);
    page1_fensi(&pcf);
    page1_time(&pct);

    lv_add_compment(&c22,&c33);
    lv_add_compment(&c33,&pcf);
    lv_add_compment(&pcf,&pct);
    lv_add_compment(&pct,&pcth);

    obj_move(&c22,1);
    ANIEND
}
void page1_end()
{
    //ESP_LOGI("SYSTEM","正在退出界面0");
    lv_task_del(task_time);    
    lv_task_handler();
    //ESP_LOGI("SYSTEM","删除任务");

    obj_move(&c22,0);
    ANIEND
    //ESP_LOGI("SYSTEM","移动");
    // free(img_b22n);
    // free(img_b33n);
    // free(imgtianqi);
    //ESP_LOGI("SYSTEM","释放内存");
    lv_obj_clean(scr_body);//删除scr_body
    ESP_LOGI("SYSTEM","esp_get_free_heap_size : %d  ", esp_get_free_heap_size());
    
}
void page1_start()
{
    page1_load();
    task_time = lv_task_create(page_task_time, 500, LV_TASK_PRIO_MID, NULL); //创建任务
    //ESP_LOGI("SYSTEM","esp_get_free_heap_size : %d ", esp_get_free_heap_size());
}
